package com.appspot.yozmcup;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;

@SuppressWarnings("serial")
public class CheckIdServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
		String userid = req.getParameter("id");
		DatastoreService dataStore = DatastoreServiceFactory.getDatastoreService();
		Query q = new Query("Member");
		q.addFilter("userid", Query.FilterOperator.EQUAL, userid);
		PreparedQuery pq = dataStore.prepare(q);
		int cnt = pq.countEntities(FetchOptions.Builder.withLimit(1));
				
		String result = "";
		if(cnt==0){
			result = "{\"status\": 200, \"result_msg\": \"사용가능한 아이디입니다.\"}";
		}else{
			result = "{\"status\": 400, \"result_msg\": \"사용중인 아이디입니다.\"}";
		}
		res.setContentType("text/xml");
		res.setCharacterEncoding("UTF-8");
        res.getWriter().write(result);
	}
}
